package com.honey.spring;

import java.util.Properties;

import org.java.plugin.PluginLifecycleException;
import org.w3c.dom.Element;

import com.honey.configureation.ConfigurationPlugin;
import com.honey.configureation.holder.ConfigurationHolder;
import com.honey.configureation.xmlparser.ParseElement;
import com.honey.core.NullPlugin;
import com.honey.core.utils.ObjectFactory;
import com.honey.core.utils.StringUtility;

public class SpringPlugin extends NullPlugin implements ConfigurationHolder {
	
	@Override
	protected void doStop() throws Exception {
		System.out.println("Stop Spring");
	}

	private String applicationContext = null;
	
	@Override
	public void parserElement(Element element, Properties properties) {
		applicationContext = ParseElement.getConfiguration(element, "ApplicationContext", properties);
	}

	@Override
	public String toXmlElement() {
		return null;
	}

	@Override
	public void validate() {

	}

	/**
	 * @return the applicationContext
	 */
	public final String getApplicationContext() {
		if( applicationContext == null ){
			ConfigurationPlugin configurationPlugin;
			try {
				configurationPlugin = (ConfigurationPlugin)ObjectFactory.getRequestPlugin(this, "honey.configuration.plugin");
				Properties p = configurationPlugin.getContextInstant().getSystemProperties() ;
				if( p != null){
					applicationContext = StringUtility.mergeDirectoryPath( p.getProperty("localpath"), "ProjectWorkeySpace" );
					applicationContext =  applicationContext +"/src/spring-context/ApplicationContext.xml";
				}
			} catch (PluginLifecycleException e) {
				e.printStackTrace();
			}
		}
		
		return applicationContext;
	}
	
}
